package com.ccdt.tv.smartrecommend.utils;

import android.content.Context;
import android.util.Log;

import com.ccdt.tv.smartrecommend.model.bean.RecordBean;
import com.ccdt.tv.smartrecommend.model.db.dao.DaoManager;
import com.ccdt.tv.smartrecommend.model.db.dao.RecordBeanDao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by syt on 2018/9/3.
 * 观看记录数据库 工具类
 */

public class RecordDBUtil {
    private DaoManager manager;
    Context context;
    ArrayList<RecordBean> mRecordAllList;

    private static RecordDBUtil INSTANCE;


    public static RecordDBUtil getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new RecordDBUtil(context);
        }
        return INSTANCE;
    }


    public RecordDBUtil(Context context) {
        this.context = context;
        manager = DaoManager.getInstance();
        manager.init(context);
        if (mRecordAllList == null) {
            mRecordAllList = new ArrayList<>();
        }

    }

    /**
     * 完成对数据库 RecordBean 表的插入操作 \n
     * 只能存储20条，如果大于20条，则删除最老的一条数据后再插入新数据
     * 删除重复数据
     *
     * @return
     */
    public boolean insert(RecordBean recordBean) {
        boolean flag = false;
        try {
            ArrayList<RecordBean> recordBeans = (ArrayList<RecordBean>) listAll();
            if (recordBeans.size() >= 20) {
                Log.w("syt", "insert: 删除老数据：" + recordBeans.get(0).toString());
                deleteByID(recordBeans.get(0).getId());
            }
            for (RecordBean bean : recordBeans){
//                if(bean.getMzName().equals(recordBean.getMzName())
//                        && bean.getDetailUrl().equals(recordBean.getDetailUrl())){
                if(bean.getId().equals(recordBean.getId())){
                    Log.w("syt", "insert: 删除重复数据");
                    deleteByID(bean.getId());
                }
//                else{
//                    flag = getRecordDao().insert(recordBean) != -1;
//                }
            }
            flag = getRecordDao().insertOrReplace(recordBean) != -1;
        } catch (Exception e) {
            e.printStackTrace();

        }
        return flag;
    }


    /**
     * 插入多条记录，需要开辟新的线程
     *
     * @return
     */
    public void insertMulthRecord(final List<RecordBean> recordBeans) {
        for (RecordBean recordBean : recordBeans) {
            getRecordDao().insert(recordBean);
        }
    }

    /**
     * 刪除所有
     */
    public void deleteAll() {
        getRecordDao().deleteAll();
    }

    /**
     * 根据ID刪除单条数据
     *
     * @return
     */
    public boolean delete(RecordBean recordBean) {
        boolean flag = false;
        try {
            //按照指定的id进行删除 delete from Car where _id = ?
            getRecordDao().delete(recordBean);
            //manager.getDaoSession().deleteAll();//删除所有的记录
            flag = true;
            Log.w("syt", "delete: 是否成功：" + flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 刪除单条数据
     *
     * @return
     */
    public boolean deleteByID(long id) {
        boolean flag = false;
        try {
            //按照指定的id进行删除 delete from RECORD_BEAN where DETAIL_URL = ?
            String sql = "delete from RECORD_BEAN where ID ="+id;
            manager.getDaoSession().getDatabase().execSQL(sql);
            //manager.getDaoSession().deleteAll();//删除所有的记录
            flag = true;
            Log.w("syt", "delete: 是否成功：" + flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 返回所有记录
     *
     * @return
     */
    public List<RecordBean> listAll() {
        List list = getRecordDao().loadAll();
        Collections.reverse(list); //逆序输出
        return list;

    }

    /**
     * 获取dao对象
     *
     * @return
     */
    private RecordBeanDao getRecordDao() {
        return manager.getDaoSession().getRecordBeanDao();
    }


}
